﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using DataModel;
using System.Data.Linq;
namespace iTrafficServer
{
    public class iTrafficCloud : DataService<DataModel.iTrafficEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
             config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
             config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        [WebGet]
        public IQueryable<HotPoints_Photos> getPhotosByHotPointId(int id)
        {         
            //int year = System.DateTime.Today.Year;
            //int month = System.DateTime.Today.Month;
            //int day = System.DateTime.Today.Day;

            iTrafficEntities context = this.CurrentDataSource;
            //var photo = (from p in context.HotPoints_Photos
            //            where p.HotPoint_Id == id && 
            //            Convert.ToString(p.UploadTime.Value.Year) == year.ToString() &&
            //            Convert.ToString(p.UploadTime.Value.Month) == month.ToString() &&
            //            Convert.ToString(p.UploadTime.Value.Day) == day.ToString()           
            //            orderby p.UploadTime descending
            //            select p).Take(3);
            var photo = (from p in context.HotPoints_Photos
                         where p.HotPoint_Id == id
                         orderby p.UploadTime descending
                         select p).Take(4);
            return photo;
          }

        [WebGet]
        public IQueryable<HotPoints> getHotPointById(int id)
        {

            iTrafficEntities context = this.CurrentDataSource;

            var hotpoint = from p in context.HotPoints
                         where p.Id == id
                         select p;
            return hotpoint;
        }

    }
}
